home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 123 / MacAddict_123_2006_11.iso / Software / Productivity / iClip lite 2.wdgt / js / animator.js < prev    next >
Text File  |  2006-06-14  |  5KB  |  129 lines

  1. /*
  2. ¬© Copyright 2005 Apple Computer, Inc. All rights reserved.
  3.  
  4. IMPORTANT:  This Apple software and the associated images located in
  5. /System/Library/WidgetResources/AppleClasses/ (collectively "Apple Software")
  6. are supplied to you by Apple Computer, Inc. (‚ÄúApple‚Äù) in consideration of your
  7. agreement to the following terms. Your use, installation and/or redistribution
  8. of this Apple Software constitutes acceptance of these terms. If you do not
  9. agree with these terms, please do not use, install, or redistribute this Apple
  10. Software.
  11.  
  12. In consideration of your agreement to abide by the following terms, and subject
  13. to these terms, Apple grants you a personal, non-exclusive license, under
  14. Apple‚Äôs copyrights in the Apple Software, to use, reproduce, and redistribute
  15. the Apple Software, in text form (for JavaScript files) or binary form (for
  16. associated images), for the sole purpose of creating Dashboard widgets for Mac
  17. OS X.
  18.  
  19. If you redistribute the Apple Software, you must retain this entire notice and
  20. the warranty disclaimers and limitation of liability provisions (last two
  21. paragraphs below) in all such redistributions of the Apple Software.
  22.  
  23. You may not use the name, trademarks, service marks or logos of Apple to endorse
  24. or promote products that include the Apple Software without the prior written
  25. permission of Apple. Except as expressly stated in this notice, no other rights
  26. or licenses, express or implied, are granted by Apple herein, including but not
  27. limited to any patent rights that may be infringed by your products that
  28. incorporate the Apple Software or by other works in which the Apple Software may
  29. be incorporated.
  30.  
  31. The Apple Software is provided on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
  32. EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
  33. NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  34. REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION
  35. WITH YOUR PRODUCTS.
  36.  
  37. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE
  41. APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45.  
  46. // Classes //
  47.  
  48.     // Animator //
  49.     
  50.         function Animator(duration, interval, optionalFrom, optionalTo, optionalCallback) {
  51.             this.startTime = 0;
  52.             this.duration = duration;
  53.             this.interval = interval;
  54.             this.animations = new Array;
  55.             this.timer = null;
  56.             this.oncomplete = null;
  57.             this._firstTime = true;
  58.             var self = this;
  59.             this.animate = function(self) {
  60.                 function limit_3(a, b, c) {
  61.                     return a < b ? b : (a > c ? c : a);
  62.                 }
  63.                 var T, time;
  64.                 var ease;
  65.                 var time = (new Date).getTime();
  66.                 var dur = self.duration;
  67.                 var done;
  68.                 T = limit_3(time - self.startTime, 0, dur);
  69.                 time = T / dur;
  70.                 ease = 0.5 - (0.5 * Math.cos(Math.PI * time));
  71.                 done = T >= dur;
  72.                 var array = self.animations;
  73.                 var c = array.length;
  74.                 var first = self._firstTime;
  75.                 if (done) {
  76.                     self.stop();
  77.                     if (self.oncomplete != null)
  78.                         self.oncomplete();
  79.                 }
  80.                 for (var i = 0; i < c; ++i)
  81.                     array[i].doFrame (self, ease, first, done, time);
  82.                 self._firstTime = false;
  83.             }
  84.             if (optionalFrom !== undefined && optionalTo !== undefined && optionalCallback !== undefined)
  85.                 this.addAnimation(new Animation (optionalFrom, optionalTo, optionalCallback));
  86.         }
  87.         
  88.         Animator.prototype.start = function() {
  89.             if (this.timer == null) {
  90.                 var timeNow = (new Date).getTime();
  91.                 var interval = this.interval;
  92.                 this.startTime = timeNow - interval;
  93.                 this.timer = setInterval (this.animate, interval, this);
  94.             }
  95.         }
  96.         
  97.         Animator.prototype.stop = function () {
  98.             if (this.timer != null) {
  99.                 clearInterval(this.timer);
  100.                 this.timer = null;
  101.             }
  102.         }
  103.         
  104.         Animator.prototype.addAnimation = function (animation) {
  105.             this.animations[this.animations.length] = animation;
  106.         }
  107.     
  108.     // Animation //
  109.     
  110.         function Animation(from, to, callback) {
  111.             this.from = from;
  112.             this.to = to;
  113.             this.callback = callback;
  114.             this.now = from;
  115.             this.ease = 0;
  116.             this.time = 0;
  117.         }
  118.         
  119.         Animation.prototype.doFrame = function(animation, ease, first, done, time) {
  120.             var now;
  121.             if (done)
  122.                 now = this.to;
  123.             else
  124.                 now = this.from + (this.to - this.from) * ease;
  125.             this.now = now;
  126.             this.ease = ease;
  127.             this.time = time;
  128.             this.callback (animation, now, first, done);
  129.         }